package com.example.redis.service.impl;

import com.example.redis.dao.UserMapper;
import com.example.redis.entity.User;
import com.example.redis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    @CacheEvict(value = "user",key = "#id")
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Cacheable(value = "user",key = "#id")
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @CachePut(value = "user",key = "#record.id")
    public User updateByPrimaryKey(User record) {
        userMapper.updateByPrimaryKey(record);
        return userMapper.selectByPrimaryKey(record.getId());
    }
}
